Search


📜 [專欄新文章] 使用ethereum browser計算gas cost
...

  • Share this:


📜 [專欄新文章] 使用ethereum browser計算gas cost
✍️ Brian Po-han Chen
📥 歡迎投稿: https://medium.com/taipei-ethereum-meetup #徵技術分享文 #使用心得 #教學文 #medium
因為CW大大覺得remix團隊一直有在更新,所以建議我使用remix來做估算gas cost,之前腦子裡一直有個疑問沒有釐清楚,所以乾脆做個小實驗來驗證一下.

remix瀏覽器下方有個執行的log頁面,可以detail以及debug,非常方便呢

有gas cost的地方有兩處,transaction cost以及 execution cost,這兩個有什麼不同呢?可以參考一下他們的source code.

簡單說明一下:transaction cost指的是說將交易送至ethereum blockchain所耗費的cost,基於data size的大小,部署合約時就是基於合約內容大小.execution cost指的是說虛擬機(VM)執行所需的cost,而在部署合約時,會去執行建構子以及一些初始化的工作.

這邊做了一個簡單的合約實驗

contract Test { bytes32 public tmp; function test( bytes32 input, uint num ) constant returns (bytes32){ bytes32 result = input; for(uint i = 0; i < num; i++) { result = sha3(result); } } function set(bytes32 input, uint num) { tmp = test(input, num); }}

如果直接呼叫constant function的話因為是由本身節點去計算不會更改到區塊鏈上的值,是不會消耗gas的,但是如果是由一個一般合約(非constant function call)去呼叫一個constant function的話,因為讓礦工幫忙計算constant function,所以會消耗gas.

上面的簡單合約中,我讓test函式對第一個bytes32參數做sha3,第二個uint參數代表做幾次loop,我分別對set函式跟test函式代入10以及1000的參數,結果如下.

set(“0x63d7db5ce060b288ecf5390594d5969bc1a206ceeb24df31cffcc8876df5e44b”, 10)transaction cost:30628execution cost:6988

set(“0x63d7db5ce060b288ecf5390594d5969bc1a206ceeb24df31cffcc8876df5e44b”, 1000)transaction cost:196022execution cost:172318

test(“0x63d7db5ce060b288ecf5390594d5969bc1a206ceeb24df31cffcc8876df5e44b”, 10)transaction cost:25663 (cost only applies when called by a contract)execution cost:2023 (cost only applies when called by a contract)

test(“0x63d7db5ce060b288ecf5390594d5969bc1a206ceeb24df31cffcc8876df5e44b”, 1000)transaction cost:191057(cost only applies when called by a contract)execution cost:167353(cost only applies when called by a contract)

大致上就是這樣.不過暫時不知道為什麼當參數設定成1000時,也會造成transaction cost的提高.(不負責任猜測:transaction cost可能包含execution cost,也一並包含在最後要付給miner的fee)

使用ethereum browser計算gas cost was originally published in Taipei Ethereum Meetup on Medium, where people are continuing the conversation by highlighting and responding to this story.

👏 歡迎轉載分享鼓掌


Tags:

About author
not provided
We have regular meeting twice per month on discussing blockchain technology, smart contracts and DApps development! We would love to have you join us!
View all posts